package com.northwind.serviceImpl;

import com.northwind.annotation.AddCache;
import com.northwind.annotation.AddLog;
import com.northwind.annotation.DelCache;
import com.northwind.dao.UserMapper;
import com.northwind.entity.User;
import com.northwind.entity.UserExample;
import com.northwind.service.UserService;
import com.northwind.util.AliyunOSSUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service

public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer rows) {
        HashMap<String, Object> map = new HashMap<>();
        //前台接收的参数 page当前页 rows每页展示的数据量
        //后台返回 page当前页 records总条数 total总页数 rows数据集合
        map.put("page",page);
        //创建条件对象
        UserExample userExample = new UserExample();
        //总条数
        int records = userMapper.selectCountByExample(userExample);
        map.put("records",records);
        //总页数=总条数/每页展示条数，除不尽+1
        int total = records % rows == 0 ?records/rows:records/rows+1;
        map.put("total",total);
        //分页查询数据  1.条件 2.分页对象
        List<User> users = userMapper.selectByExampleAndRowBounds(userExample, new RowBounds((page - 1) * rows, rows));
        map.put("rows",users);
        return map;
    }

    @DelCache
    @AddLog(value = "修改用户")
    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);

    }


    @AddLog(value = "添加用户")
    @Override
    public String add(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setWechat(user.getPhone());
        user.setStatus("1");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        try {
            Date nowTimeDate = simpleDateFormat.parse(nowTime);
            user.setRegistTime(nowTimeDate);
            user.setRegistTimeString(nowTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        userMapper.insert(user);
        return user.getId();
    }

    @DelCache
    @AddLog(value = "删除用户")
    @Override
    public void delete(User user) {
        //https://videochannel.oss-cn-beijing.aliyuncs.com/headImg/1640161262960-%24%25IB%40T1FPFTKH5E%60F2W3V6N.jpg
        User user1 = userMapper.selectByPrimaryKey(user.getId());
        System.out.println("要删除的文件为:"+user1.getHeadImg());
        String[] split = user1.getHeadImg().split("/");
        //https:    videochannel.oss-cn-beijing.aliyuncs.com    headImg    640161262960-%24%25IB%40T1FPFTKH5E%60F2W3V6N.jpg
        String objectName = split[3] + "/" +split[4];
        System.out.println("映射到OSS的文件名:"+objectName);
        AliyunOSSUtil.deleteFileByAliyun("videochannel",objectName);
        userMapper.deleteByPrimaryKey(user.getId());
    }

    @Override
    public HashMap<String, Object> fileUploadByAliyun(MultipartFile headImg, String userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            //获取文件名
            String originalFilename = headImg.getOriginalFilename();
            String newFilename = System.currentTimeMillis()+"-"+originalFilename;
            String objectName = "headImg/"+newFilename;
            AliyunOSSUtil.uploadByteFileAliyun("videochannel",objectName,headImg);

            //修改文件信息
            User user = new User();
            user.setId(userId);
            System.out.println("要修改的userId:"+userId);
            System.out.println("数据库headImg对应oss路径:https://videochannel.oss-cn-beijing.aliyuncs.com/"+objectName);
            user.setHeadImg("https://videochannel.oss-cn-beijing.aliyuncs.com/"+objectName);
            //https://videochannel.oss-cn-beijing.aliyuncs.com/headImg/1640161262960-%24%25IB%40T1FPFTKH5E%60F2W3V6N.jpg
            userMapper.updateByPrimaryKeySelective(user);
            hashMap.put("message","文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            hashMap.put("message","文件上传失败");
        }
        return hashMap;
    }

    @Override
    public Integer totalByMonth(String dateString) {
        UserExample userExample = new UserExample();
        //userExample.createCriteria()
        userExample.createCriteria().andRegistTimeStringLike(dateString);
        int i = userMapper.selectCountByExample(userExample);
        return i;
    }

    @Override
    public Integer total() {
        UserExample userExample = new UserExample();
        int i = userMapper.selectCountByExample(userExample);
        return i;
    }
}
